package com.molyfun.parents.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class SexChooseView extends LinearLayout implements View.OnClickListener{

    private int mChooseType = SexType.TYPE_MALE;

    public static interface SexType{
        public static final int TYPE_MALE = 1001;
        public static final int TYPE_FEMALE = 1002;
    }

    protected LayoutInflater layoutInflater;

    private Context context;
    private boolean inited;

    @InjectView(R.id.sex_male_layout)
    RelativeLayout mMaleLayout;
    @InjectView(R.id.male_choose_box)
    ImageView mMaleChooseBox;
    @InjectView(R.id.sex_female_layout)
    RelativeLayout mFemaleLayout;
    @InjectView(R.id.female_choose_box)
    ImageView mFemaleChooseBox;


    public SexChooseView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public SexChooseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SexChooseView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_sex_choose, this);
        ButterKnife.inject(this);
        mMaleLayout.setOnClickListener(this);
        mFemaleLayout.setOnClickListener(this);
    }

    //0:男 1:女
    public String getCurrentSex(){
        if(mChooseType == SexType.TYPE_FEMALE){
            return "1";
        }else{
            return "0";
        }
    }

    //0:男 1:女
    public void setCurrentSex(String sex){
        if(TextUtils.isEmpty(sex) || sex.equals("0")){
            setTabClick(SexType.TYPE_MALE);
        }else{
            setTabClick(SexType.TYPE_FEMALE);
        }
    }

    public int getCurrentChooseTab(){
        return mChooseType;
    }

    /**
     * init view
     *
     */
    public void init() {
        if(inited){
            return;
        }
        inited = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sex_male_layout:
                setTabClick(SexType.TYPE_MALE);
                break;
            case R.id.sex_female_layout:
                setTabClick(SexType.TYPE_FEMALE);
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == SexType.TYPE_MALE){
            mMaleChooseBox.setImageResource(R.mipmap.ic_checkbox_checked);
            mFemaleChooseBox.setImageResource(R.mipmap.ic_checkbox_unchecked);
            mChooseType = SexType.TYPE_MALE;
        }else{
            mMaleChooseBox.setImageResource(R.mipmap.ic_checkbox_unchecked);
            mFemaleChooseBox.setImageResource(R.mipmap.ic_checkbox_checked);
            mChooseType = SexType.TYPE_FEMALE;
        }
    }

}